<?php

/**
 * Test views: for diagnostics, etc.
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
class ViewTests {

    public function __construct() {
    }

    public static function mailTest() {
        $language = new Language();
        $language->load("en", "code");
        echo "Language is: " . $language->name . " or " . $language->getCode() . ". ";

        $address = new Address();
        $address->load(1, "id", TRUE);
        echo "Address is: " . $address->address1 . " or " . $address->getZip() . ". ";

        $database = Database::getInstance();
        // $output = $database->queryArray(new Query("show columns from person;"));
        Utils::dump($address, FALSE);

        $address = new Address(array(
            "address1" => "b1",
            "address2" => "b2",
            "address3" => "b3",
            "city" => "b4",
            "county" => "b5",
            "zip" => "b6",
            "country" => 225
        ));
        $address->save();
        Utils::dump($address, FALSE);

        $mail = new Mail(
                    "Test email",
                    "This is just a test email, to see if it works.",
                    FALSE,
                    new EmailAddress("ian.clarence@gmail.com", "Ian Clarence"),
                    new EmailAddress("noreply@iclarence.com", "IRC3"),
                    new EmailAddress("noreply@iclarence.com", "IRC3"),
                    new EmailAddress("noreply@iclarence.com", "IRC3")
                );
        if ($mail->sendMail()) {
            echo "Mail worked!!";
        }
        else {
            echo "Mail fail!";
        }
    }

    public static function testForm() {
        $title = ViewHtmlElement::elementInit("title", "IRC3: Front end");

        $head = ViewHtmlElement::elementInit("head", $title);
        $author = new ViewMeta();
        $httpEquiv = new ViewMeta(NULL, ViewMeta::CHAR_SET . CHARSET, ViewMeta::CONTENT_TYPE);
        $head->addInnerHtml($author);
        $head->addInnerHtml($httpEquiv);
        // $head->addInnerHtml(new ViewLink("theme.css"));
        // $head->addInnerHtml(new ViewScript("theme.js"));

        $link = ViewHtmlElement::elementInit("a", "link", "href", "http://www.yahoo.com");
        $link->setAttribute("target", "_blank");
        $link->setAttribute("title", "Link to yahoo.com");
        $link->addClass("link");
        $link->addClass("hidden");

        $heading = ViewHtmlElement::elementInit("h1", "Hello World");

        $paragraph = ViewHtmlElement::elementInit("p", "This is the first test. To see if it works. This is a ");
        $paragraph->addInnerHtml($link);
        $paragraph->addText(" to Yahoo.");
        $paragraph->setId("para1");

        $input = new ViewInputText(NULL, "test", "test");
        $input->setLabel("this is a test");
        $input->setRequired();

        $hidden = new ViewInputText(NULL, "x", "x", "3", TRUE);

        $textarea = new ViewTextArea(NULL, "textarea", "textarea", "Here is some text in a text area. More text. More text again.");
        $textarea->setLabel("this is a text");

        $form = new ViewForm("myform", "myform", "#", TRUE);
        $form->addInnerHtml($input);
        $form->addInnerHtml($hidden);
        $form->addInnerHtml($textarea);

        $titleArray = array("Mr", "Mrs", "Miss", "Ms");
        $selectElement = new ViewSelect(NULL, "title", "title", $titleArray);
        $selectElement->setLabel("title");
        $form->addInnerHtml($selectElement);

        $langArr = array(array("EN", "English"), array("FR", "French"), array("DE", "German"), array("ES", "Spanish"), array("IT", "Italian"));
        $langDefArr = array("id" => "lang", "name" => "language", "default_value" => "ES", "label" => "language selection", "select_message" => "Please select a language");
        $langDef = new HtmlFormElement($langDefArr, $langArr);

        $lang = new ViewRadio($langDef);
        $form->addInnerHtml($lang);

        // $lang2Def = new HtmlFormElement(array("id" => "lang", "name" => "language", "default_value" => "ES"), $langArr);
        $lang2 = new ViewSelect($langDef);
        // $lang2->setLabel("Languages");
        // $lang2->disable();
        $form->addInnerHtml($lang2);

        $checkbox = new ViewCheckbox(NULL, "signup", "signedup", "yes", "no");
        $checkbox->setLabel("Have you signed up yet?");
        $form->addInnerHtml($checkbox);

        $button = new ViewButton(NULL, NULL, "button", "OK");
        $form->addInnerHtml($button);

        $submit = new ViewSubmit(NULL, NULL, "button", "OK");
        $form->addInnerHtml($submit);
        $form = $form->encapsulate();

        $body = ViewHtmlElement::elementInit("body", $heading);
        $body->addInnerHtml($paragraph);
        $body->addInnerHtml($form);
        $body->addBr();
        $body->addP("Here is another string of text.", TRUE);

        $tableArray = array(array("1", "2", "3"), array("A", array($paragraph), "C"), array("D", "E", "F"));
        $table = new ViewTable("mytable", "MyTable", $tableArray);
        $table->addClassToColumn(1, "specialColumn");
        $table->addClassToRow(2, "specialRow");
        $body->addInnerHtml($table);

        $listArray = $tableArray[1];
        $ol = new ViewOl("list", FALSE, $listArray);
        $ol->addClassToListItem(2, "specialLi");
        $body->addInnerHtml($ol);

        $html = new ViewHtml($head, $body);
        return $html->render();
    }

    public static function listPages() {
        $title = ViewHtmlElement::elementInit("title", "IRC3: Front end");

        $head = ViewHtmlElement::elementInit("head", $title);
        $author = new ViewMeta();
        $httpEquiv = new ViewMeta(NULL, ViewMeta::CHAR_SET . CHARSET, ViewMeta::CONTENT_TYPE);
        $head->addInnerHtml($author);
        $head->addInnerHtml($httpEquiv);

        $heading = ViewHtmlElement::elementInit("h2", "Testing of IRC3");
        $body = ViewHtmlElement::elementInit("body", $heading);

        $requestUri = new RequestUri();
        $pageSequence = $requestUri->getPageSequence();
        $pageNumber = $requestUri->getPageNumber();

        $hostHeading = ViewHtmlElement::elementInit("h3", "HTTP Host");
        $hostSummary = ViewHtmlElement::elementInit("p", Utils::getHost());
        $secure = ViewHtmlElement::elementInit("p", (Utils::getPageSecure() ? "Page secure" : "Page not secure"));
        $body->addInnerHtml($hostHeading);
        $body->addInnerHtml($hostSummary);
        $body->addInnerHtml($secure);

        $pageHeading = ViewHtmlElement::elementInit("h3", "This Page");
        // $page = Page::getPage($requestUri->getPage());
        $page = $requestUri->getPage();
        $pageType = $page->getPageType();
        $pageSummary = ViewHtmlElement::elementInit("p", $page->getSummary());
        $pageTypeSummary = ViewHtmlElement::elementInit("p", $pageType->getSummary());
        $body->addInnerHtml($pageHeading);
        $body->addInnerHtml($pageSummary);
        $body->addInnerHtml($pageTypeSummary);

        $parentHeading = ViewHtmlElement::elementInit("h3", "Parent Page");
        $page->fetchParent();
        $parent = $page->getParent();
        if ($parent instanceof Page) {
            $parent->load($parent->getId());
            $parentSummary = ViewHtmlElement::elementInit("p", $parent->getSummary());
        }
        else {
            $parentSummary = ViewHtmlElement::elementInit("p", "No parent page.");
        }
        $body->addInnerHtml($parentHeading);
        $body->addInnerHtml($parentSummary);

        $childHeading = ViewHtmlElement::elementInit("h3", "Child Pages");
        $page->fetchChildren();
        $children = Page::getList($page->getChildren());
        $childList = is_array($children) ? new ViewOl("list", FALSE, $children) : ViewHtmlElement::elementInit("p", "No child pages.");
        $body->addInnerHtml($childHeading);
        $body->addInnerHtml($childList);

        $siblingHeading = ViewHtmlElement::elementInit("h3", "Sibling Pages");
        $page->fetchSiblings();
        $siblings = Page::getList($page->getSiblings());
        $siblingList = is_array($siblings) ? new ViewOl("list", FALSE, $siblings) : ViewHtmlElement::elementInit("p", "No sibling pages.");
        $body->addInnerHtml($siblingHeading);
        $body->addInnerHtml($siblingList);

        $primaryHeading = ViewHtmlElement::elementInit("h3", "Primary Pages");
        $primaries = Page::getList($page->getPrimaries(TRUE));
        $primaryList = is_array($primaries) ? new ViewOl("list", FALSE, $primaries) : ViewHtmlElement::elementInit("p", "No primary pages.");
        $body->addInnerHtml($primaryHeading);
        $body->addInnerHtml($primaryList);

        $descendantHeading = ViewHtmlElement::elementInit("h3", "Descendant Pages");
        $page->fetchDescendants();
        $descendants = Page::getList($page->getDescendants());
        $descendantList = is_array($descendants) ? new ViewOl("list", FALSE, $descendants) : ViewHtmlElement::elementInit("p", "No descendant pages.");
        $body->addInnerHtml($descendantHeading);
        $body->addInnerHtml($descendantList);

        $ancestorHeading = ViewHtmlElement::elementInit("h3", "Ancestor Pages");
        $page->fetchAncestors();
        $ancestors = Page::getList($page->getAncestors());
        $ancestorList = is_array($ancestors) ? new ViewOl("list", FALSE, $ancestors) : ViewHtmlElement::elementInit("p", "No ancestor pages.");
        $body->addInnerHtml($ancestorHeading);
        $body->addInnerHtml($ancestorList);

        $primaryHeading = ViewHtmlElement::elementInit("h3", "Panels");
        $panels = Panel::getList($page->getPanels());
        $panelList = is_array($panels) ? new ViewOl("list", FALSE, $panels) : ViewHtmlElement::elementInit("p", "No panels.");
        $body->addInnerHtml($primaryHeading);
        $body->addInnerHtml($panelList);

        $html = new ViewHtml($head, $body);
        return $html->render();
    }

}